//
//  UserViewController.m
//  CharlesMVX
//
//  Created by Charles on 2020/3/11.
//  Copyright © 2020 Charles. All rights reserved.
//

#import "MVCUserViewController.h"
#import "UserInfoView.h"
#import "Masonry.h"
#import "MVCUserInfoController.h"
#import "MVCBlogListController.h"
#import "MVCUserViewController+Protect.h"
#import "DetailViewController.h"
#import "MVCInfoViewController.h"

#define LoginUserId 123

@interface MVCUserViewController (){
    NSInteger _count;
}

@property (nonatomic, assign) NSUInteger userId;
@property (nonatomic, strong) UserInfoView *userInfoView;
@property (nonatomic, strong) MVCUserInfoController *userInfoController;
@property (nonatomic, strong) MVCBlogListController *blogListController;

@property (nonatomic, copy) void(^RetainCycleBlock)(void);


@end

@implementation MVCUserViewController

- (void)dealloc
{
    NSLog(@"%@ dealloc",NSStringFromClass(self.class));
}

+ (instancetype)instanceWithUserId:(NSUInteger)userId {
    if (userId == LoginUserId) {
        return [[MVCInfoViewController alloc]initWithUserId:userId];
    }else {
        return [[MVCUserViewController alloc]initWithUserId:userId];
    }
}

- (instancetype)initWithUserId:(NSUInteger)userId {
    if (self = [super init]) {
        self.userId = userId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configuration];
    [self addUI];
    [self fetchData];
}

- (void)configuration {
    self.title = [NSString stringWithFormat:@"用户%@", @(self.userId)];
    self.userInfoController = [MVCUserInfoController instanceWithUserId:self.userId];
    [self.userInfoController setNavigationController:self.navigationController];
    [self.userInfoController setVCGenerator:^UIViewController *(id params) {
        return [[DetailViewController alloc]init];
    }];
    
    self.blogListController = [MVCBlogListController instanceWithUserId:self.userId];
    [self.blogListController setVCGenerator:^UIViewController *(id params) {
        return [[DetailViewController alloc]init];
    }];
}

- (void)addUI {
    self.userInfoView = self.userInfoController.userInfoView;
    self.userInfoView.backgroundColor = [UIColor lightGrayColor];
    UITableView *tableView = self.blogListController.tableView;
    [self.view addSubview:self.userInfoView];
    [self.view addSubview:tableView];
    
    [self.userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.inset(60);
        make.left.right.inset(0);
        make.height.mas_equalTo(260);
    }];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.top.equalTo(self.userInfoView.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}

- (void)fetchData {
    [self.userInfoController fetchData];
    [self showHUD];
    [self.blogListController fetchDataWithCompletionHandler:^(NSError *error, id result) {
        [self hideHUD];
    }];
}

@end
